package com.army.study;

import com.army.study.database.CardDB;
import com.army.study.database.DBUtils;
import com.army.study.database.Form;
import com.army.study.service.TimerAbilityService;
import com.army.study.slice.MainAbilitySlice;
import com.army.study.utils.ComponentProviderUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentProvider;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;

public class MainAbility extends Ability {
    private static final String TAG = MainAbility.class.getSimpleName();

    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private static final int DEFAULT_DIMENSION_2X4 = 3;
    private static final int INVALID_FORM_ID = -1;
    private static final String EMPtY_STRING = "";

    private long formId;
    private ProviderFormInfo formInfo;
    private DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        connect = helper.getOrmContext("CardDB", "CardDB.db", CardDB.class);
        // 启动Timer Ability Service
        Intent intentService = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(TimerAbilityService.class.getName())
                .build();
        intentService.setOperation(operation);
        startAbility(intentService);

        super.setMainRoute(MainAbilitySlice.class.getName());
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        if (intent == null) {
            return new ProviderFormInfo();
        }

        // 获取卡片id
        formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        }else {
            return new ProviderFormInfo();
        }

        // 获取卡片名称
        String formName = EMPtY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }

        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }
        int layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_2;
        if (dimension == DEFAULT_DIMENSION_2X4) {
            layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_4;
        }

        formInfo = new ProviderFormInfo(layoutId, this);
        // 存储卡片信息
        Form form = new Form(formId, formName, dimension);
        ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(form, this);
        formInfo.mergeActions(componentProvider);
        if (connect == null) {
            connect = helper.getOrmContext("CardDB", "CardDB.db", CardDB.class);
        }

        try {
            DBUtils.insert(form, connect);
        } catch (Exception e) {
            DBUtils.delete(form.getFormId(), connect);
        }

        return formInfo;
    }

    @Override
    protected void onDeleteForm(long formId) {
        super.onDeleteForm(formId);
        // 删除数据库中的卡片信息
        DBUtils.delete(formId, connect);
    }
}
